package com.google.code.sysarch.common.util;

/*
 * ============================================================================
 * file:	$RCSfile: ThreadGraphicOutput.java,v $
 * created by:Frank Drewek
 *
 * last modification at $Date: 1997/10/16 13:52:30 $
 *		   by $Author: jschneid $
 *
 * (c)	Universitaet Karlsruhe
 *	Institut fuer Betriebs- und Dialogsysteme, Lehrstuhl Betriebssysteme
 *	Am Fasanengarten 5, 76128 Karlsruhe
 *
 * Permission to use this software for non-profit educational purposes is
 * hereby granted. All other rights are reserved. This software is provided
 * "as is", without any express or implied warranty.
 * ============================================================================
 */

// package threads;

import java.awt.*;

import com.google.code.sysarch.v0.e37.ThreadBuffer;


/**
 * ThreadGraphicOutput is a class that manages the graphical output
 * of the buffer from class ThreadBuffer. It interprets the values
 * of the buffer as the length of a filled rectangle and prints the
 * rectangles on the screen.
 *
 * @author    Frank Drewek
 * @author    Juergen Schneider
 * @see       ThreadTA7
 * @see       FileGraphicProdThread
 * @see       ThreadBuffer
 * @see       Buffer 
 */
public class ThreadGraphicOutput1 extends Canvas
 {
   ////////////////////////////////////////////////////////////////////////////
   // attributes
   ////////////////////////////////////////////////////////////////////////////

   /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

/** Object that contains the outer iteration-number. */
   private ThreadBuffer threadBuffer;
   
   /** object for double buffering */
   private Image offscreenImage;
   
   /** Object that contains the graphics. It is used for double buffering. */
   private Graphics offscreenGraph;

   /** Object that contains the panel. */
   private Frame frame;
   
   /** Object that contains the graphics. */
   private Panel panel;
   
   /** width of the canvas */
   private int width;
   
   /** height of the canvas */
   private int height;
   
   /** white color */
   static final Color WHITE = Color.white;
   
   /** red color */
   static final Color RED = Color.red;

   /** state is true if the object for double buffering is initialized. */
   private boolean state;
   
   ////////////////////////////////////////////////////////////////////////////
   // constructors
   ////////////////////////////////////////////////////////////////////////////

   /**
    * The constructor ThreadGraphicOutput initializes the width and height
    * of the frame that will be printed on the screen.
    * It gets the buffer from which it takes the length for the rectangles.
    * It creates a frame that shows the graphics on the screen.
    * @param b   central object from class ThreadBuffer
    * @param wi  width of the canvas
    * @param hi  height of the canvas
    */
   public ThreadGraphicOutput1(ThreadBuffer b, int wi, int hi)
     {
       threadBuffer = b;
       width = wi;
       height = hi;
       setSize(width, height);
       setBackground(WHITE);
	   
       state = true;
       frame = new Frame("View");
       frame.setSize(width+20, height+20);
	   
       panel = new Panel();
       panel.setLayout(new GridLayout(1, 1));
       panel.add(this);
	   
       frame.add(panel);
       frame.setVisible(true); 
     }
 

   ////////////////////////////////////////////////////////////////////////////
   // methods
   ////////////////////////////////////////////////////////////////////////////

   // access //////////////////////////////////////////////////////////////////


   // service /////////////////////////////////////////////////////////////////

   
   /** paint shows the graphics on the screen. */
   public synchronized void paint(Graphics g)
     {
       if (state == true)
	 {
	   offscreenImage = createImage(width, height);
	   offscreenGraph = offscreenImage.getGraphics();
           state = false;
         }

       offscreenGraph.setColor(WHITE);
       offscreenGraph.fillRect(0, 0, width, height);
       
       for (int i=0; i < threadBuffer.getMax(); i++)
         {
           offscreenGraph.setColor(RED);
           offscreenGraph.fillRect((10 + 30*i), 10, 20, threadBuffer.getBuffer(i));
         }   

       g.drawImage(offscreenImage, 0, 0, this);
     }

   /**
    * update invokes the paint-method 
    * @param g    the graphics that will be printed
    */
   public synchronized void update(Graphics g)
     {
       paint(g);
     }

   /**
    * refreshView invokes the method update.
    * If the buffer is empty the application will end.
    */
   public synchronized void refreshView()
     {
       update(getGraphics());

       if (threadBuffer.isEmpty() == true)
         {
	   frame.setVisible(false);
	   System.exit(0);
         }
     }

   // debug ///////////////////////////////////////////////////////////////////


   /** @deprecated	for debugging purposes */
   public final static String revision_GraphicBufferThread =
     "$Id: ThreadGraphicOutput.java,v 1.2 1997/10/16 13:52:30 jschneid Exp drewek $";

   /** @deprecated	for debugging purposes */
   public String getRevision()
     { return revision_GraphicBufferThread; }
 }



 
